package com.itheima.case1;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Case5Runnable implements Runnable{
    static int[] j = {10,5,20,50,100,200,500,800,2,80,300,700};
    static int count = 0;

    Random random = new Random();
    static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while(true){
            lock.lock();
            try{
                if(count == j.length){
                    break;
                }
                Thread.sleep(10);
                int i = random.nextInt(j.length);
                if(j[i] != 0){
                    System.out.println(Thread.currentThread().getName() +"又产生了一个"+j[i]+"元大奖");
                    j[i] = 0;
                    count++;
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }

    }
}
